/*
 * Logger.cpp
 *
 *  Created on: Oct 25, 2012
 *      Author: johnny
 */

#include "Logger.h"
#include "util.h"

#include <boost/format.hpp>

namespace core {

Logger& Logger::getInstance() {
	static Logger logger;
	return logger;
}

Logger::Logger() {
}

Logger::~Logger() {
	log.clear();
}

void Logger::add(core::ErrorType errType, std::string message, std::wstring sender) {
    std::wstring wMessage(message.begin(), message.end());
    add(errType, wMessage, sender);
}

void Logger::add(core::ErrorType errType, std::wstring message, std::wstring sender) {
    std::wstring msg = (boost::wformat(L"<entry><type=%1%><message=\"%2%\"></entry>") % errType % message).str();

    boost::shared_ptr<LogEntry> entry(new LogEntry);
    entry->timestamp = timestamp();
    entry->message = msg;
    entry->type = errType;
    entry->sender = sender;

    log.push_back(entry);
    notifyAll(static_cast<void*>(entry.get()));
}

void Logger::addObserver(patterns::Observer *obs) {
    LogEntry entry = {0, L"INIT", L"", Debug};

    patterns::Observable::addObserver(obs);
    obs->update(this, (void*)&entry);
}

const LogList& Logger::getFullLog() {
    return log;
}

} /* namespace core */
